﻿@if (string.IsNullOrWhiteSpace(Text))
{
    @if (string.IsNullOrWhiteSpace(Tooltip))
    {
        <MButton Outlined="Outlined" Href="@Href" Target="@Target" Icon="!Fab" Fab="Fab" Small="Small||((!XSmall)&&Fab)" Color="@Color" Class="@Class" OnClick="OnButtonClick" Loading="loading" Disabled="Disabled" XSmall="XSmall" StopPropagation="StopPropagation" Plain="Plain" Text="TextStyle">
            <MIcon XSmall="XSmall">@Icon</MIcon>
        </MButton>

    }
    else
    {
        <MTooltip Bottom>
         <ActivatorContent>
             <MButton Outlined="Outlined" Href="@Href" Target="@Target" Icon="!Fab" Small="Small||((!XSmall)&&Fab)" Fab="Fab" Color="@Color" Class="@Class" @attributes="@context.Attrs" OnClick="OnButtonClick" Loading="loading" Disabled="Disabled" XSmall="XSmall" StopPropagation="StopPropagation" Plain="Plain" Text="TextStyle">
                 <MIcon XSmall="XSmall">@Icon</MIcon>
             </MButton>
         </ActivatorContent>
         <ChildContent>
             <span>@Tooltip</span>
         </ChildContent>
     </MTooltip>
    }

}
else
{
    @if (string.IsNullOrWhiteSpace(Tooltip))
    {
        <MButton Outlined="Outlined" XLarge="XLarge" Href="@Href" Target="@Target" Color="@Color" Class="@Class" OnClick="OnButtonClick" Loading="loading" Small="Small" Disabled="Disabled" Rounded="Rounded" Depressed="Depressed" XSmall="XSmall" StopPropagation="StopPropagation" Plain="Plain" Text="TextStyle">
            @if (string.IsNullOrWhiteSpace(Icon) == false)
            {
                <MIcon XSmall="XSmall" Large="XLarge" Class="@(XLarge?"me-4":"")" Left="!string.IsNullOrWhiteSpace(Text)">@Icon</MIcon>
            }

            @Text
        </MButton>
    }
    else
    {
        <MTooltip Bottom>
         <ActivatorContent>
             <MButton Outlined="Outlined" XLarge="XLarge" Target="@Target" Href="@Href" Color="@Color" Class="@Class" @attributes="@context.Attrs" OnClick="OnButtonClick" Loading="loading" Small="Small" Disabled="Disabled" Rounded="Rounded" Depressed="Depressed" XSmall="XSmall" StopPropagation="StopPropagation" Plain="Plain" Text="TextStyle">
                 @if (string.IsNullOrWhiteSpace(Icon) == false)
                    {
                        <MIcon XSmall="XSmall" Large="XLarge" Class="@(XLarge?"me-4":"")">@Icon</MIcon>
                    }

                    @Text
                </MButton>
            </ActivatorContent>
            <ChildContent>
                <span>@Tooltip</span>
            </ChildContent>
        </MTooltip>
    }

}
@code {
    [Parameter]
    public string Icon { get; set; }
    [Parameter]
    public string Href { get; set; }
    [Parameter]
    public string Target { get; set; }
    [Parameter]
    public string Class { get; set; }
    [Parameter]
    public bool Outlined { get; set; }
    [Parameter]
    public bool StopPropagation { get; set; }
    [Parameter]
    public bool IsAsync { get; set; }
    [Parameter]
    public bool Fab { get; set; }
    [Parameter]
    public bool Small { get; set; }
    [Parameter]
    public bool XSmall { get; set; }
    [Parameter]
    public bool XLarge { get; set; }
    [Parameter]
    public string Text { get; set; }
    [Parameter]
    public string Tooltip { get; set; }
    [Parameter]
    public string Color { get; set; } = "primary";
    [Parameter]
    public bool Disabled { get; set; }
    [Parameter]
    public bool Depressed { get; set; }
    [Parameter]
    public bool TextStyle { get; set; }
    [Parameter]
    public bool Rounded { get; set; }
    [Parameter]
    public bool Plain { get; set; }
    [Parameter]
    public EventCallback OnClick { get; set; }

    private bool loading { get; set; }

    public async Task OnButtonClick()
    {
        if (IsAsync)
        {
            loading = true;
            StateHasChanged();
            await OnClick.InvokeAsync();
            loading = false;
            StateHasChanged();
        }
        else
        {
            await OnClick.InvokeAsync();
        }
    }
}
